﻿using System.Collections.Generic;

namespace ObserverPattern
{
    /// <summary>
    /// subject 主题目录
    /// </summary>
    internal class Subject
    {
        private List<ObServer> observers = new List<ObServer>();

        private int state;

        public int GetState()
        {
            return state;
        }

        public void SetState(int _state)
        {
            this.state = _state;
            NotifyAllObservers();
        }

        public void Attach(ObServer observer)
        {
            observers.Add(observer);
        }

        public void Detach(ObServer observer)
        {
            observers.Remove(observer);
        }

        public void NotifyAllObservers()
        {
            foreach (var item in observers)
            {
                item.Update();
            }
        }

    }
}
